package com.lhy.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lhy.domain.ChatTea;
import com.lhy.domain.Page;
import com.lhy.domain.Result;
import com.lhy.domain.Student;
import com.lhy.service.IUserService;

@Controller
@RequestMapping("/chatTea")
public class ChatTeaController {
	@Autowired
	private IUserService userService;

	@RequestMapping("")
	public ModelAndView list(String pageNow){
		Integer pagenow = 1;
		if(!StringUtils.isEmpty(pageNow)){
			pagenow = Integer.valueOf(pageNow);
		}
		ModelAndView mv = new ModelAndView();
		try {
			userService.deleteOverdueChatTea();
			Integer count = userService.queryAllChatTeaCount();
			Page page = new Page(count, pagenow);
			Integer startIndex = pagenow*page.getPageSize()-3;
			Integer endIndex = pagenow*page.getPageSize();
			List<ChatTea> list = userService.queryChatTeaBypage(startIndex,endIndex);
//			List<ChatTea> list = new ArrayList<>();
//			list = userService.queryAllChatTea();
//			if(list.size()>0){
//				mv.addObject("list",list);
//			}
			mv.addObject("list",list);
			mv.addObject("page",page);
			mv.setViewName("chatTea");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	@RequestMapping("/do")
	public @ResponseBody
	Map<String, Object> chat(String usersequence, String content)  {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		Result result=new Result();
		result.setCode("1");
		result.setDes("成功");
		
		try{
			Student s =userService.queryStudentBySequence(Integer.valueOf(usersequence));
			userService.saveChatTeaContent(Integer.valueOf(usersequence),s.getNickname(),content);
		}catch(Exception e){
				result.setCode("0");
				result.setDes(e.getMessage());
			
		}
		returnMap.put("result", result);
		
		return returnMap;
	}
}
